<template>
  <div>
    <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
      <el-form-item label="所属社团" prop="clubId">
        <el-select v-model="formData.clubId" placeholder="请选择所属社团" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in clubOptions" :key="index" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="活动标题" prop="title">
        <el-input v-model="formData.title" placeholder="请输入活动标题" clearable :style="{width: '100%'}"></el-input>
      </el-form-item>
      <el-form-item label="活动描述" prop="description">
        <el-input v-model="formData.description" type="textarea" placeholder="请输入活动描述" :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
      </el-form-item>
      <el-form-item label="活动时间" prop="activityTime">
        <el-date-picker v-model="formData.activityTime" format="yyyy-MM-dd" value-format="yyyy-MM-dd" :style="{width: '100%'}" placeholder="请选择活动时间" clearable></el-date-picker>
      </el-form-item>
      <el-form-item label="活动地点" prop="location">
        <el-input v-model="formData.location" placeholder="请输入活动地点" clearable :style="{width: '100%'}"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status" size="medium">
          <el-radio-button v-for="(item, index) in statusOptions" :key="index" :label="item.value">{{ item.label }}</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item size="large">
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { addActivity } from '@/api/club/activity'
import { listInfo } from '@/api/club/info'
export default {
  components: {},
  props: [],
  data() {
    return {
      formData: {
        clubId: undefined,
        title: '',
        description: '',
        activityTime: null,
        location: '',
        status: 0,
      },
      rules: {
        clubId: [{ required: true, message: '请选择所属社团', trigger: 'change' }],
        title: [{
          required: true,
          message: '请输入活动标题',
          trigger: 'blur'
        }],
        description: [{
          required: true,
          message: '请输入活动描述',
          trigger: 'blur'
        }],
        activityTime: [{ required: true, message: '请选择活动时间', trigger: 'change' }],
        location: [{
          required: true,
          message: '请输入活动地点',
          trigger: 'blur'
        }],
        status: [{ required: true, message: '状态不能为空', trigger: 'change' }],
      },
      clubOptions: [],
      statusOptions: [
        { label: '预告', value: 0 },
        { label: '已结束', value: 1 }
      ],
    }
  },
  computed: {},
  watch: {},
  created() {
    this.loadClubOptions()
  },
  mounted() {},
  methods: {
    submitForm() {
      this.$refs['elForm'].validate(valid => {
        if (!valid) return
        addActivity(this.formData).then(() => {
          this.$modal.msgSuccess('新增成功')
          this.resetForm()
        })
      })
    },
    resetForm() {
      this.$refs['elForm'].resetFields()
    },
    loadClubOptions() {
      const query = { pageNum: 1, pageSize: 999 }
      listInfo(query).then(res => {
        const rows = res.rows || []
        this.clubOptions = rows.map(item => ({ label: item.name || `社团${item.id}`, value: item.id }))
      }).catch(() => {
        this.clubOptions = []
        this.$message && this.$message.error('无法加载社团列表，请检查权限')
      })
    },
  }
}

</script>
<style>
</style>
